package itsm.isperp.module.controller.incident;

import itsm.isperp.framework.utils.JsonBuilder;
import itsm.isperp.framework.web.controller.BaseController;
import itsm.isperp.framework.web.response.ResultMessage;
import itsm.isperp.module.entity.incident.IcdIncidentProperty;
import itsm.isperp.module.service.incident.IcdIncidentPropertyService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 控制器
 * 
 * @author lizx
 * @date 2016-07-21 18:06
 */
@Controller
@RequestMapping("incident/property")
public class IcdIncidentPropertyController extends
		BaseController<IcdIncidentProperty> {

	@Autowired
	protected IcdIncidentPropertyService icdIncidentPropertyService;

	@Override
	public IcdIncidentPropertyService getService() {
		return this.icdIncidentPropertyService;
	}

	@Override
	public String getPageTitle() {
		return "扩展字段";
	}

	/**
	 * 根据id获取值
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("listProperty")
	@ResponseBody
	public List<Map<String,Object>> listProperty(String id) {
		return this.getService().getProperty(id);
	}

	@RequestMapping("saveProperty")
	@ResponseBody
	public ResultMessage saveProperty(String relationId, HttpServletRequest req) {

		String propertyList = req.getParameter("propertyList");
		IcdIncidentProperty[] propertyArr = JsonBuilder.buildNormalBinder()
				.fromJson(propertyList, IcdIncidentProperty[].class);

		List<IcdIncidentProperty> list = new ArrayList<IcdIncidentProperty>();

		if (propertyArr != null) {
			for (IcdIncidentProperty item : propertyArr) {
				list.add(item);
			}
		}

		icdIncidentPropertyService.deleteByRelationId(relationId);

		// 记录变更日志
		for (IcdIncidentProperty cpi : list) {
			cpi.setRelationId(relationId);
			icdIncidentPropertyService.saveOrUpdate(cpi);
		}

		return ResultMessage.success();

	}

}
